/**
 * 
 */
package gui.view.source;

import gui.controller.SourceLeftListener;
import gui.controller.SourceMainListener;
import gui.view.CustomizedColor;

import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.GroupLayout.ParallelGroup;
import javax.swing.GroupLayout.SequentialGroup;
import javax.swing.table.DefaultTableModel;

/**
 * @author Elisa Costante
 * 
 */
public class SourceLeftPanel extends JPanel {

	private static final long serialVersionUID = 8599098410604311828L;
	private JLabel numberOfSourcesLabel;
	private JComboBox numberOfSourcesCombo;
	private JScrollPane tableSourcesScrollPane;
	private JButton editButton;
	private JTable tableSources;
	private DefaultTableModel sourcesTableModel;
	private JButton loadButton;
	private JButton saveButton;
	private JButton showDataButton;
	private SourceLeftListener leftListener;
	private SourceMainListener mainListener;

	public SourceLeftPanel() {
		super();
	}

	public void init(SourceLeftListener leftListener,
			SourceMainListener mainListener) {
		this.leftListener = leftListener;
		this.mainListener = mainListener;
		this.setBackground(CustomizedColor.BACK_GROUND_COLOR);

		setLeftComponents();
		setLeftPanelLayout();

	}

	private void setLeftPanelLayout() {
		GroupLayout layout = new GroupLayout(this);
		this.setLayout(layout);
		layout.setAutoCreateGaps(true);
		layout.setAutoCreateContainerGaps(true);

		ParallelGroup horizontal = layout.createParallelGroup();

		SequentialGroup upGroup = layout.createSequentialGroup();
		upGroup.addComponent(numberOfSourcesLabel, 100, 200, Short.MAX_VALUE);
		upGroup.addComponent(numberOfSourcesCombo, 200, 300, Short.MAX_VALUE);

		SequentialGroup downGroup = layout.createSequentialGroup();
		downGroup.addComponent(tableSourcesScrollPane, 100, 200,
				Short.MAX_VALUE);

		ParallelGroup buttonsGroup = layout.createParallelGroup();
		buttonsGroup.addComponent(loadButton, 100, 100, 100);
		buttonsGroup.addComponent(saveButton, 100, 100, 100);
		buttonsGroup.addComponent(editButton, 100, 100, 100);
		buttonsGroup.addComponent(showDataButton, 100, 100, 100);

		downGroup.addGroup(buttonsGroup);

		horizontal.addGroup(upGroup);
		horizontal.addGroup(downGroup);

		SequentialGroup vertical = layout.createSequentialGroup();

		ParallelGroup leftVerticalGroup = layout.createParallelGroup();
		leftVerticalGroup.addComponent(numberOfSourcesLabel, 30, 30, 30);
		leftVerticalGroup.addComponent(numberOfSourcesCombo, 30, 30, 30);

		SequentialGroup buttonsVerticalGroup = layout.createSequentialGroup();
		buttonsVerticalGroup.addComponent(loadButton, 30, 30, 30);
		buttonsVerticalGroup.addComponent(saveButton, 30, 30, 30);
		buttonsVerticalGroup.addComponent(editButton, 30, 30, 30);
		buttonsVerticalGroup.addComponent(showDataButton, 30, 30, 30);

		ParallelGroup downVerticalGroup = layout.createParallelGroup();
		downVerticalGroup.addComponent(tableSourcesScrollPane, 300, 300,
				Short.MAX_VALUE);
		downVerticalGroup.addGroup(buttonsVerticalGroup);

		vertical.addGroup(leftVerticalGroup);
		vertical.addGroup(downVerticalGroup);

		layout.setHorizontalGroup(horizontal);
		layout.setVerticalGroup(vertical);
	}

	private void setLeftComponents() {
		numberOfSourcesLabel = new JLabel("Number of Sources");

		numberOfSourcesCombo = new JComboBox(new Integer[] { 1, 2, 3, 4, 5, 6,
				7, 8, 9, 10 });
		numberOfSourcesCombo.setActionCommand("numberOfSource");
		numberOfSourcesCombo.addActionListener(leftListener);

		tableSourcesScrollPane = new JScrollPane(getSourcesTable());
		getSourcesTable().setFillsViewportHeight(true);

		editButton = new JButton("Edit");
		editButton.setActionCommand("editSource");
		editButton.addActionListener(mainListener);

		loadButton = new JButton("Load");
		loadButton.setActionCommand("load");
		loadButton.addActionListener(leftListener);

		saveButton = new JButton("Save");
		saveButton.setActionCommand("save");
		saveButton.addActionListener(leftListener);

		showDataButton = new JButton("Show Data");
		showDataButton.setActionCommand("showDataButton");
		showDataButton.addActionListener(mainListener);

	}

	public JTable getSourcesTable() {
		if (tableSources == null) {
			sourcesTableModel = new DefaultTableModel(new String[] {
					"Previous Name", "New Name" }, 0);
			tableSources = new JTable(sourcesTableModel);
			// Add the listener to change into the table
			tableSources.getModel().addTableModelListener(leftListener);
			tableSources.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

			// It's only possible select the rows
			tableSources.setRowSelectionAllowed(true);
			tableSources.setColumnSelectionAllowed(false);

			// Add a Row Selection Listener
			tableSources.getSelectionModel().addListSelectionListener(
					mainListener);
		}
		return tableSources;
	}

	/**
	 * @return the numberOfSourcesLabel
	 */
	public JLabel getNumberOfSourcesLabel() {
		return this.numberOfSourcesLabel;
	}

	/**
	 * @return the numberOfSourcesCombo
	 */
	public JComboBox getNumberOfSourcesCombo() {
		return this.numberOfSourcesCombo;
	}

	/**
	 * @return the tableSourcesScrollPane
	 */
	public JScrollPane getTableSourcesScrollPane() {
		return this.tableSourcesScrollPane;
	}

	/**
	 * @return the editButton
	 */
	public JButton getEditButton() {
		return this.editButton;
	}

	/**
	 * @return the sourcesTableModel
	 */
	public DefaultTableModel getSourcesTableModel() {
		return this.sourcesTableModel;
	}
}
